package com.daisuxia.web.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.web.common.reslult.Status;
import com.daisuxia.web.pojo.JsAwardRecord;
import com.daisuxia.web.pojo.JsDrawrollsRecord;
import com.daisuxia.web.pojo.JsStepRecord;
import com.daisuxia.web.pojo.WebResultJson;
import com.daisuxia.web.service.IJsAwardRecordService;
import com.daisuxia.web.service.IJsDrawrollsRecordService;
import com.daisuxia.web.service.IJsLoanPersonService;
import com.daisuxia.web.service.IJsStepConfigService;
import com.daisuxia.web.service.IJsStepRecordService;
import com.daisuxia.web.service.JsAwardRecordService;
import com.daisuxia.web.service.JsDrawrollsRecordService;
import com.daisuxia.web.service.JsLoanPersonService;
import com.daisuxia.web.service.JsStepConfigService;
import com.daisuxia.web.service.JsStepRecordService;
import com.daisuxia.web.util.JSONUtil;
import com.daisuxia.web.util.PageConfig;

 
@Controller
@RequestMapping("/jsaward/awardCenterWeb/")
 public class AwardCenterWebController  extends BaseController{
	private static Logger loger = Logger.getLogger(IndexController.class);

	@Autowired
	private IJsLoanPersonService jsLoanPersonService;
	@Autowired
	private IJsAwardRecordService jsAwardRecordService;
	@Autowired
	private IJsDrawrollsRecordService jsDrawrollsRecordService;
	@Autowired
	private IJsStepConfigService jsStepConfigService;
	@Autowired
	private IJsStepRecordService jsStepRecordService;
	@Autowired
	private AwardCenterController awardCenterController;

	
	@RequestMapping(value = {"drawAwardList", ""})
	public void drawAwardList(HttpServletRequest request, HttpServletResponse response,Model model) {

		HashMap<String, Object> mapParams = new HashMap<String, Object>();
		Map models = new HashMap();
		JSONObject json=new JSONObject();
		String code="";
		String msg="";
		try {
			Integer pageNum = Integer.parseInt(request.getParameter("pageNo"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageSize"));
			JsAwardRecord jsAwardRecord = new  JsAwardRecord();
			jsAwardRecord.setStatusItem("statusItem");
			mapParams.put("statusItem", "statusItem");
			mapParams.put("pageNum", pageNum);
			mapParams.put("numPerPage", numPerPage);
			PageConfig<JsAwardRecord>  page = jsAwardRecordService.findPageList(mapParams);
			List<JsAwardRecord> list= page.getItems();
			request.setAttribute("list", list);
			models.put("pageTotal", page.getTotalPages());
			models.put("pageNo",page.getCurrentPage());
			models.put("pageSize", page.getPageSize());
			models.put("list", list);
			json.put("list", models);
		} catch (Exception e) {
			loger.error("drawAwardList  choujiang  error", e);
		}
		json.put("code",code);
		json.put("message",msg);
		JSONUtil.toObjectJson(response, json.toString());
	}

	@RequestMapping(value = {"userDrawAwardList", ""})
	public void userDrawAwardList(HttpServletRequest request, HttpServletResponse response,Model model) {
		 Map<String, Object> json = new HashMap<String, Object>();
		 String code = Status.FAILD.getName();
	        String msg = "获取失败";
		HashMap<String, Object> mapParams = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
			mapParams = getParametersO(request);
			String phone = String.valueOf(request.getParameter("phone"));
			Integer pageNum = Integer.valueOf(request.getParameter("page").toString());
			Integer numPerPage = Integer.valueOf(request.getParameter("pageSize").toString());
			JsDrawrollsRecord jsDrawrollsRecord = new JsDrawrollsRecord();
			Map models = new HashMap();
				if(!StringUtils.isNotBlank(phone)){
					code = "500";
					msg = "参数错误，请检查参数！";
					loger.info("参数问题：" + mapParams.toString());					 
					return;				
				}
				jsDrawrollsRecord.setPhone(phone);
				HashMap<String, Object> params = new HashMap<String, Object>();
				params.put("phone", phone);
				params.put("pageNum", pageNum);
				params.put("numPerPage", numPerPage);
				PageConfig<JsDrawrollsRecord>  page =jsDrawrollsRecordService.findListByUserPhone(params);
				List<JsDrawrollsRecord> pageList= page.getItems();
				if(pageList != null && pageList.size() > 0){
					Map<String, Object> dataMap = null;
					for(JsDrawrollsRecord map : pageList){
						dataMap = new HashMap<String, Object>();
						dataMap.put("awardId", map.getId());
						dataMap.put("userId", map.getUserId());
						dataMap.put("periods", map.getPeriods());
						dataMap.put("awardMoney", map.getAwardMoney());
						dataMap.put("luckyDraw", map.getLuckyDraw());
						dataMap.put("stepName", map.getStepName());
						dataMap.put("status", map.getStatus());
						dataMap.put("addTime", map.getAddTime());
						dataList.add(dataMap);
					}
				}
				models.put("pageTotal", page.getTotalPageNum());
				models.put("page", page.getCurrentPage());
				models.put("pageSize", page.getPageSize());
				models.put("item", dataList);
				code = "0";
				msg = "查询成功！";
				json.put("data", models);
		} catch (Exception e) {
			loger.error("userDrawAwardList  choujiang  error", e);
			  code = Status.FAILD.getName();
		      msg = "系统异常";
		} finally {
			 json.put("code", code);
	         json.put("message", msg);
	         JSONUtil.toObjectJson(response, JSONUtil.beanToJson(json));
		}		
	}
	/**
	 * 滚动展示中奖信息
	 * @param request
	 * @param response
	 * @param model
	 */
	
	@RequestMapping(value = {"drawAwardIndexList", ""})
	public void drawAwardIndexList(HttpServletRequest request, HttpServletResponse response,Model model) {
		Map<String, Object> json = new HashMap<String, Object>();
		String code = Status.FAILD.getName();
	    String msg = "获取失败";
		HashMap<String, Object> mapParams = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
			mapParams = getParametersO(request);			
			
//			Integer pageNo = Integer.valueOf(request.getParameter("page").toString());
//			Integer pageSize = Integer.valueOf(request.getParameter("pageSize").toString());
			
			Integer pageNo = 1;
			Integer pageSize = 50;
			JsDrawrollsRecord jsDrawrollsRecord = new JsDrawrollsRecord();
			Map models = new HashMap();
			JsStepRecord jsStepRecord = new JsStepRecord();
			JsAwardRecord jsAwardRecord = new JsAwardRecord(); 
//			PageConfig<JsStepRecord>  page= null;
					//jsStepRecordService.findPageList(pageNo,pageSize , jsStepRecord);
			jsAwardRecord =awardCenterController.getCurrentjsAwardRecord();

//				List<JsStepRecord> pageList=page.getItems();
//				if(pageList != null && pageList.size() > 0){
//					Map<String, Object> dataMap = null;
//					for(JsStepRecord jsr : pageList){
//						if(jsr.getEffectiveCount()!=null&&jsr.getEffectiveCount()>0){
//							if(jsr.getJsLoanPerson()!=null){								
//							
//							dataMap = new HashMap<String, Object>();
//							dataMap.put("awardId", jsr.getId());
//							dataMap.put("userId", jsr.getUserId());
//							dataMap.put("userName","尾号"+jsr.getJsLoanPerson().getPhone().substring(7,jsr.getJsLoanPerson().getPhone().length()));
//							dataMap.put("stepId", jsr.getStepId());
//							dataMap.put("effectiveCount", jsr.getEffectiveCount());
//							//dataMap.put("periods", jsr.getJsAwardRecord().getPeriods());
//							dataMap.put("stepName", jsr.getJsStepConfig().getStepName());
//							dataList.add(dataMap);
//							}
//						}
//					}
//				}
				
				BigDecimal awardMoney=new BigDecimal(0);
				models.put("item", dataList);
				if(jsAwardRecord!=null){
					awardMoney=jsAwardRecord.getAwardMoney();
				}
				models.put("awardMoney", awardMoney);
				code="0";
				msg="查询成功";
				//serviceResult.setExt("123");
				json.put("data", models);
		} catch (Exception e) {
			loger.error("drawAwardIndexList  choujiang  error", e);
			code = Status.FAILD.getName();
			msg="查询失败";
		} finally {
			 json.put("code", code);
	         json.put("message", msg);
	         JSONUtil.toObjectJson(response, JSONUtil.beanToJson(json));
		}		
	}
}
